package com.woniuxy.book87.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.book87.entity.Book;
import com.woniuxy.book87.mapper.BookMapper;
import com.woniuxy.book87.mapper.BookTypeMapper;
import com.woniuxy.book87.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BookService {

    @Autowired
    BookMapper bookMapper ;

    public PageInfo<BookVo> findBookByPage(Integer now, Integer size, Integer typeId, String name) {
        PageHelper.startPage(now,size);
        List<BookVo> lists = bookMapper.selectBookByNameAndType(typeId,name);
        return new PageInfo<>(lists);
    }

    public void add(Book book) {
        book.setCreatetime(new Date());
        book.setCollectioncount(0);
        book.setReadcount(0);
        book.setBuycount(0);
        book.setState(1);
        book.setVersion(0);
        //book.setImgsrc("111");

        bookMapper.insert(book);
    }

    public Book findBookByKey(Long id) {
        return  bookMapper.selectByPrimaryKey(id);
    }

    public void modify(Book book) {
        Book book1 = bookMapper.selectByPrimaryKey(book.getId());
        book.setUpdatetime(new Date());
        bookMapper.updateByPrimaryKey(book);
    }
}
